//
//  MemberSendMsgViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 16/6/1.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "MemberSendMsgViewController.h"
#import "UIViewExt.h"
#import "RDVTabBarController.h"
#import "HttpRequest.h"
@interface MemberSendMsgViewController ()<UITextViewDelegate,UIGestureRecognizerDelegate,UIScrollViewDelegate>

@end

@implementation MemberSendMsgViewController
{   UIScrollView*mainScroll;
    UITextView* tv;
    UILabel * label;
    UIImageView*imagess;
    UIButton* Leavemessage;
}
@synthesize alt = alt;
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    [[self rdv_tabBarController] setTabBarHidden:YES];
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"发短信";
    
    // 手势
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [self.view addGestureRecognizer:singleFingerOne];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     
                                                                     UITextAttributeFont,nil]];
    
    self.view. backgroundColor=[UIColor colorWithRed:255/255.00 green:255/255.00 blue:255/255.00 alpha:1.0];
    mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight)];
    
    [self.view addSubview:mainScroll];
    [mainScroll setBackgroundColor:[UIColor whiteColor]];
    mainScroll.delegate = self;
    mainScroll.userInteractionEnabled = YES;
    mainScroll.scrollEnabled = YES;
   
    UILabel *how = [[UILabel alloc]initWithFrame:CGRectMake(0, 0 , ScreenWidth, 40)];
    how.text =[NSString stringWithFormat:@"    已选择 %ld 位会员",self.memberIds.count];
    how.textColor=zhonghuiColor;
    how.backgroundColor=[UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1];
    how .font = [UIFont systemFontOfSize:13];
    [mainScroll addSubview:how];
    
    tv=[[UITextView alloc]init];
    tv.frame=CGRectMake(15, how.bottom+20, ScreenWidth-30, 150);
    tv.backgroundColor = [UIColor whiteColor];//设置它的背景颜色/Users/jiezsoft/Desktop/HJB_project  /HJB_project.xcodeproj
    tv.scrollEnabled = YES;//是否可以拖动
    tv.delegate=self;
    //    tv.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高
    tv.font = [UIFont fontWithName:@"Arial" size:15.0];//设置字体名字和字体大小
    tv.layer.borderWidth =0.3;
    CGColorSpaceRef registedSpace11 = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref111 = CGColorCreate(registedSpace11,(CGFloat[]){ 208/255.0, 208/255.0, 208/255.0, 1.0 });
    [tv.layer setBorderColor:tureBTncolorref111];//边框颜色
    tv.layer.cornerRadius =0;
    [mainScroll addSubview:tv];
    
    imagess=[[UIImageView alloc]initWithFrame:CGRectMake(8, 10, 51/3, 43/3)];
    imagess.image=[UIImage imageNamed:@"_0000_xingxifankui.png"];
    [tv addSubview:imagess];
    label=[[UILabel alloc]init];
    label.frame =CGRectMake(28, 11, ScreenWidth-60, 14);
    label.text = @"请输入短信内容";
    label.textColor=qianhuiColor;
    label.font=[UIFont systemFontOfSize:14];
    [tv addSubview:label];
    Leavemessage = [[UIButton alloc] initWithFrame:CGRectMake(15, tv.bottom+64+70, ScreenWidth-30, 45)];
    [Leavemessage setTitle:@"发送" forState:UIControlStateNormal];
    [Leavemessage setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [Leavemessage.layer setCornerRadius:3.0]; //设置矩圆角半径
    [Leavemessage.layer setBorderWidth:0.3];   //边框宽度
    CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){228/255.0, 228/255.0, 228/255.0, 1.0});
    [Leavemessage.layer setBorderColor:tureBTncolorref];//边框颜色
    Leavemessage.backgroundColor=qianhuiColor;
    [self.view addSubview:Leavemessage];
    
    
}
-(void)textViewDidChange:(UITextView *)textView
{
   
    if (textView.text.length == 0) {
        [imagess removeFromSuperview];
      
        imagess=[[UIImageView alloc]initWithFrame:CGRectMake(8, 10,51/3, 43/3)];
        imagess.image=[UIImage imageNamed:@"_0000_xingxifankui.png"];
        [tv addSubview:imagess];
        label.text = @"请输入短信内容";
        [Leavemessage setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [Leavemessage.layer setCornerRadius:2.0]; //设置矩圆角半径
        [Leavemessage.layer setBorderWidth:0.3];   //边框宽度
        CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){228/255.0, 228/255.0, 228/255.0, 1.0});
        [Leavemessage.layer setBorderColor:tureBTncolorref];//边框颜色
        Leavemessage.backgroundColor=qianhuiColor;
        
        
        
    }else{
        [imagess removeFromSuperview];
        label.text = @"";
        [Leavemessage setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        Leavemessage.backgroundColor=luseColor;
        [Leavemessage.layer setCornerRadius:3.0]; //设置矩圆角半径
        [Leavemessage.layer setBorderWidth:0.3];   //边框宽度
        CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){204.0/255.0,153.0/255.0, 51.0/255.0, 1.0});
        [Leavemessage.layer setBorderColor:tureBTncolorref];//边框颜色
        [Leavemessage addTarget:self action:@selector(Leavemessage:) forControlEvents:UIControlEventTouchUpInside];
        
        
        
    }
}




-(void)Leavemessage:(UIButton*)sender{
    [tv resignFirstResponder];
    
        if ([tv.text length] <1)
        {
            [self showAlertMessage:@"信息内容不能为空！"];
        }else{
            
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString* Token = [user objectForKey:@"tokenId"];
            NSMutableDictionary *params = [NSMutableDictionary new];
            [params setValue:Token forKey:@"tokenId"];
            [params setValue:self.memberIds forKey:@"memberIds"];
            [params setValue:tv.text forKey:@"message"];
            [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    
            [HttpRequest requestData:@"Business/Member/SendMsg" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [self showAlertMessage:requestResultStr];
                        [_delegate update:@""];
                        [self performSelector:@selector(back) withObject:nil afterDelay:1.0f];
                       
                    }else{
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [Global showTextHudSub:requestResultStr view:self.view];
                       }}
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    [Global showTextHudSub:severDataDict.resultStr view:self.view];
                }
                
            }];
        }}
- (void)back{
 [self.navigationController popViewControllerAnimated:YES];
}

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定哦 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}


- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
-(void)OnTapDone{
    [tv resignFirstResponder];
    
}

//回收键盘
-(void)closeKeyBoad
{
    [tv resignFirstResponder];
    
}

@end
